package newui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shwread.android.qysw10000242.R;

/**
 * Created by lex on 2017/7/6.
 */

public class MyWorldPageAdapter extends RecyclerView.Adapter<MyWorldPageAdapter.MyWorldGridHolder> implements View.OnClickListener {

    private Context context;
    private String[] names;
    private Integer[] ids;

    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener){
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public MyWorldPageAdapter(Context context, String[] names,Integer[] ids) {
        this.context = context;
        this.names = names;
        this.ids = ids;
    }

    @Override
    public MyWorldPageAdapter.MyWorldGridHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(
                context).inflate(R.layout.item_mywordpage_category, parent,
                false);
        MyWorldGridHolder holder = new MyWorldGridHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyWorldPageAdapter.MyWorldGridHolder holder, int position) {
        holder.tvName.setText(names[position]);
        holder.ivIcon.setImageResource(ids[position]);
        holder.itemView.setTag(names[position]);
    }

    @Override
    public int getItemCount() {
        return names.length;
    }

    @Override
    public void onClick(View view) {
        onRecyclerViewItemClickListener.onItemClick(view,view.getTag());
    }

    class MyWorldGridHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private ImageView ivIcon;

        public MyWorldGridHolder(View view) {
            super(view);
            tvName = (TextView) view.findViewById(R.id.tvName);
            ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Object data);
    }
}
